package com.zijin.controller.user;

import com.baomidou.mybatisplus.core.metadata.IPage;

import com.zijin.annotation.Authority;
import com.zijin.dto.user.UserDTO;
import com.zijin.dto.user.UserEidtDTO;
import com.zijin.dto.user.UserLoginDTO;
import com.zijin.qurey.user.UserPageQureyDTO;
import com.zijin.result.PageResult;
import com.zijin.result.Result;

import com.zijin.service.user.IUserService;
import com.zijin.vo.user.UserLoginVO;
import com.zijin.vo.user.UserVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.security.auth.login.LoginException;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;

/**
 * 员工管理
 */
@RestController
@RequestMapping("/base/user")
@Slf4j
@Api(tags = "用户相关接口")
public class UserController {

    @Autowired
    private IUserService userService;

    @Resource
    @Lazy
    private RedisTemplate redisTemplate;


    /**
     * 登录
     *
     * @param userLoginDTO
     * @return
     */
    @PostMapping("/login")
    @ApiOperation(value = "登录")
    public Result<UserLoginVO> login(@RequestBody UserLoginDTO userLoginDTO, HttpServletRequest request) throws LoginException {
        return Result.success(userService.login(userLoginDTO,request));
    }

    /**
     * 退出
     *
     * @return
     */
    @PostMapping("/logout")
    @ApiOperation("退出")
    public Result<String> logout() {
        return Result.success();
    }

    /**
     * 新增员工
     * @param userDTO
     * @return
     */
    @PostMapping("/addOne")
    @ApiOperation("新增用户(管理员注册)")
    @Authority("admin")
    public Result save(@RequestBody @Valid UserDTO userDTO){
        userService.saveOne(userDTO);
        return Result.success();
    }

    /**
     * 员工分页查询
     * @param userPageQureyDTO
     * @return
     */
    @GetMapping("/getPage")
    @ApiOperation("用户分页查询")
    @Authority("admin")
    public Result<PageResult> page(UserPageQureyDTO userPageQureyDTO){
        IPage<UserVo> userVOPage = userService.pageQurey(userPageQureyDTO);
        return Result.success(new PageResult(userVOPage.getTotal(),userVOPage.getRecords()));
    }


    @GetMapping("/getList")
    @ApiOperation("用户分页查询")
    @Authority("admin")
    public Result<List<UserVo>> getList(UserPageQureyDTO userPageQureyDTO){
        return Result.success( userService.getList(userPageQureyDTO));
    }

    /**
     * 启用禁用员工账号
     * @param status
     * @param id
     * @return
     */
    @PostMapping("/status/{status}")
    @ApiOperation("启用禁用用户账号")
    @Authority("admin")
    public Result startOrStop(@PathVariable Integer status,Long id){
        return null;
    }

    /**
     * 根据id查询员工信息
     * @param id
     * @return
     */
    @GetMapping("/getOne/{id}")
    @ApiOperation("根据id查询员工信息")
    public Result<UserVo> getById(@PathVariable Long id){
        return Result.success(userService.getUserById(id));
    }

    @GetMapping("/getUser")
    @ApiOperation("查询员工信息(无需id)")
    public Result<UserVo> getUser(){
        return Result.success(userService.getUser());
    }

    /**
     * 编辑员工信息
     * @param userDTO
     * @return
     */
    @PutMapping("/edit/{id}")
    @ApiOperation("编辑员工信息")
    @Authority("admin")
    public Result<Boolean> update(@PathVariable Long id,@RequestBody UserEidtDTO userDTO){
        log.info("编辑用户信息：{}", userDTO);
        return Result.success(userService.editOne(id,userDTO));
    }


    @PutMapping("/edit")
    @ApiOperation("编辑员工信息")
    public Result<Boolean> update(@RequestBody UserEidtDTO userDTO){
        log.info("编辑用户信息：{}", userDTO);
        return Result.success(userService.edit(userDTO));
    }

    @PutMapping("/resetPassword/{id}")
    @ApiOperation("重置密码")
    public Result<Boolean> resetPassword(@PathVariable("id") Long id){
        return Result.success(userService.resetPassword(id));
    }

}
